package dq.study.minimvc.handler;

import dq.study.minimvc.reflect.HandlerMethod;
import dq.study.minimvc.reflect.ServletInvocableHandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter {


    public RequestMappingHandlerAdapter() {
    }

    @Override
    protected boolean supportsInternal(Object handler) {
        return true;
    }

    @Override
    protected Object handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
        return invokeHandlerMethod(request, response, handlerMethod);
    }

    private Object invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
        ServletInvocableHandlerMethod servletInvocableHandlerMethod = createHandlerMethod(handlerMethod);
        return servletInvocableHandlerMethod.invokeHandlerMethod(request,response,handlerMethod);
    }

    private ServletInvocableHandlerMethod createHandlerMethod(HandlerMethod handlerMethod) {
        return new ServletInvocableHandlerMethod(handlerMethod);
    }
}
